var fs = require("fs");
var http = require("http");
var gulp = require("gulp");
var _ = require("lodash");

var tempUrl = "temp/";

/**
 * 创建连续的目录
 * @param {String} path  like a/b/c
 * @param {String} base 
 */
function mkdirs(path, base) {
	path = path.replace(/\\/g, "/");
	var arr = path.split("/");
	var temp = "";
	if(base) {
		temp = base;
		if(!fs.existsSync(temp)) {
			fs.mkdirSync(temp);
		}
	}
	arr.forEach(function(value, index, list) {
		if(value !== "") temp += value + "/";
		if(!fs.existsSync(temp)) {
			fs.mkdirSync(temp);
		}
	})
	console.log("创建", path, "成功");
}

var fsTest = {
	testUrl: tempUrl + "hello",
	test0: function() {
		console.log("hello world");
	},
	test1: function() {
		fs.unlink(this.testUrl, (err) => {
			if(err) {
				throw err;
			}
			console.log("成功删除 /tmp/hello")
		})
	},
	test2: function() {
		fs.unlinkSync(this.testUrl);
		console.log('成功删除 /tmp/hello');
	},
	test3: function() {
		fs.rename(this.testUrl, tempUrl + "world", (err) => {
			if(err) throw err;
			console.log('重命名完成');
			fs.stat(tempUrl + "world", (err, stats) => {
				if(err) throw err;
				console.log(`文件属性: ${JSON.stringify(stats)}`);
			});
		});
	},
	test4: function() {
		console.log("context path :", process.cwd());
	},
	test5: function() {
		http.createServer((req, res) => {
			// 发送 HTTP 头部 
			// HTTP 状态值: 200 : OK
			// 内容类型: text/plain
			//res.writeHead(200, {'Content-Type': 'text/plain',"Specal-Status":666});
			res.end("hello world");
		}).listen(8888);
		// 终端打印如下信息
		console.log('Server running at http://127.0.0.1:8888/');
	},
	test6: function() {
		var url = tempUrl;
		if(fs.existsSync(url)) {
			console.log("路径为", url, "的文件夹已存在");
		} else {
			fs.mkdir(url, function(err, b) {
				if(err) throw err;
				console.log("创建文件夹成功")
			})
		}
	},
	test7: function() {
		var arr = ["DirectoryOfTile", "Favorites", "FocusImage", "FolderView", "HotComment", "HtmlView", "IFrame", "InfoCount", "KnowledgeMap", "Links", "Message", "Navigation", "OwnerCheckout", "OwnerUpload", "RecentComment", "RecentDownload", "RecentPreview", "RecentUpload", "Share", "SimpleSearch", "TabView", "TextList", "TopAction", "Welcome"];
		arr.forEach(function(v, i, list) {
			mkdirs(v + "/Dlls/a/b/c/d/", tempUrl)
		})
	},
	test8: function() {
		mkdirs("D:\\workspace\\icedog.javascript.test\\0nodejs\\temp\\a\\b\\c\\d")
	},
	test9: function() {
		var users = [{
				'user': 'barney',
				'age': 36
			},
			{
				'user': 'fred',
				'age': 40
			},
			{
				'user': 'pebbles',
				'age': 18
			}
		];
		var names = _.chain(users)
			.map(function(user) {
				return user.user;
			})
			.join(" , ")
			.value();
		console.log(names);
		console.log("----------");
		var youngest = _.chain(users)
			.min(function(user) {
				return user.age;
			})
			.value();
		console.log(youngest);
		console.log("----------");
		var youngest2 = _.chain(users)
			.sortBy("age")
			.map(function(user) {
				console.log("map", user);
				return user;
			})
			.first()
			.value();
		console.log(youngest2);
		console.log("----------");
	},
	test10: function() {
		var i = 0;
		http.createServer((req, res) => {
			i = i + 1;
			console.log(i, req.url);
			res.writeHead(200, {'Content-Type': 'text/plain'});
			res.end('Hello World\n');
		}).listen(8888);
		// 终端打印如下信息
		console.log('Server running at http://127.0.0.1:8888/');
	}
}

function main() {
	console.time("t");
	fsTest.test10();
	console.timeEnd("t");
}
main();